class JsonWebToken
  class << self

    def encode(payload, exp=15.minutes.from_now)
      payload[:exp] = exp.to_i
      payload[:sub] = "Miyuan SourceFinder Platform"
      payload[:iss] = "Baoji Yunfeng Technology Co., Ltd"
      JWT.encode(payload, Rails.application.secrets.secret_key_base)
    end

    def decode(token)
      body = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
      HashWithIndifferentAccess.new body
    rescue
      nil
    end
  end
end
